package com.longcai.piaojia.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.SetDefaultAddressGet;
import com.longcai.piaojia.conn.UserAddressShowGet;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by Administrator on 2016/8/3.
 */
public class ReceiveAddressListAdapter extends ArrayAdapter<UserAddressShowGet.UserAddressEntity> {

    private LayoutInflater layoutInflater;
    private Context context;
    private SetDefaultAddressListener setDefaultAddressListener;

    public ReceiveAddressListAdapter(Context context, List<UserAddressShowGet.UserAddressEntity> objects, SetDefaultAddressListener setDefaultAddressListener) {

        super(context, 0, objects);

        this.context = context;
        this.setDefaultAddressListener = setDefaultAddressListener;

        layoutInflater = ((Activity) context).getLayoutInflater();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder viewHolder;

        UserAddressShowGet.UserAddressEntity userAddressEntity = getItem(position);

        if (convertView == null) {

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_receive_address_list, null)));

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        viewHolder.item_receive_address_name_tv.setText(userAddressEntity.name);
        viewHolder.item_receive_address_phonenum_tv.setText(userAddressEntity.phone);
        viewHolder.item_receive_address_address_tv.setText(userAddressEntity.city + context.getResources().getString(R.string.space) + userAddressEntity.address);

        viewHolder.item_receive_default_address_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setDefaultAddressListener.setDefault(position);

            }
        });


        if (userAddressEntity.status.equals("1")) {

            viewHolder.item_receive_default_address_iv.setVisibility(View.VISIBLE);
            viewHolder.item_receive_default_address_circle_iv.setVisibility(View.GONE);
            viewHolder.item_receive_default_address_rl.setClickable(false);

        }else {

            viewHolder.item_receive_default_address_iv.setVisibility(View.GONE);
            viewHolder.item_receive_default_address_circle_iv.setVisibility(View.VISIBLE);
            viewHolder.item_receive_default_address_rl.setClickable(true);

        }

        viewHolder.item_receive_address_update_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setDefaultAddressListener.setUpdate(position);

            }
        });
        viewHolder.item_receive_address_delete_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setDefaultAddressListener.setDelete(position);

            }
        });

        return convertView;

    }

    private class ViewHolder {

        @BoundView(R.id.item_receive_default_address_rl) public RelativeLayout item_receive_default_address_rl;
        @BoundView(R.id.item_receive_default_address_iv) public ImageView item_receive_default_address_iv;
        @BoundView(R.id.item_receive_default_address_circle_iv) public ImageView item_receive_default_address_circle_iv;
        @BoundView(R.id.item_receive_address_name_tv) public TextView item_receive_address_name_tv;
        @BoundView(R.id.item_receive_address_phonenum_tv) public TextView item_receive_address_phonenum_tv;
        @BoundView(R.id.item_receive_address_address_tv) public TextView item_receive_address_address_tv;
        @BoundView(R.id.item_receive_address_update_ll) public LinearLayout item_receive_address_update_ll;
        @BoundView(R.id.item_receive_address_delete_ll) public LinearLayout item_receive_address_delete_ll;

    }

    public interface SetDefaultAddressListener{

        void setDefault(int pos);

        void setUpdate(int pos);

        void setDelete(int pos);

    }

}
